package com.xc.freeapp.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Goods;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.impl.GoodsService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("v3/goods")
@Api(value="v3/goods",description = "商品列表")
public class GoodsControllerV3 extends AbstractController {

	@Autowired
	private GoodsService GoodsService;
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "商品列表")
	public ResponsePage<List<Goods>> getMessage(@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit) throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		log.info("消息记录" + authInfo.getUserId() + "访问");
		PageBounds pageBounds = new PageBounds(page, limit);
		List<Goods> goodss= GoodsService.selectByHospitalid(authInfo.getAppIntId(), pageBounds);
		for (Goods goods : goodss) {
			goods.setImage(UploadUtil.urlConversion(goods.getImage()));
			goods.setPicslist(UploadUtil.pathstoList(goods.getPics()));
		}
		Paginator paginator =((PageList)goodss).getPaginator();
		return new ResponsePage<List<Goods>>(goodss, paginator);
	}

	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "商品详情")
	public Goods getMessage(@PathVariable("id") Integer id) throws BaseException {
 		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录" + authInfo.getUserId() + "访问");
		Goods goods =GoodsService.selectByPrimaryKey(id);
		goods.setImage(UploadUtil.urlConversion(goods.getImage()));
		goods.setPicslist(UploadUtil.pathstoList(goods.getPics()));
		return goods;
	}

}
